<script type="text/html" template>
  <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{layui.cache.version}}" media="all">
  <link rel="stylesheet" href="{{ layui.setter.base }}assets/css/m-global.css?v={{layui.cache.version}}" media="all">
</script>


<div class="layadmin-user-login m-page-bg m-page-bg--dark layadmin-user-display-show" id="LAY-user-login" style="display: none;">

  <div class="layadmin-user-login-main m-fixed--r">
    <div class="layadmin-user-login-box layadmin-user-login-header">
      <h2>抖贴后台管理系统</h2>
      <!-- <p>layui 官方出品的单页面后台管理模板系统</p> -->
    </div>
    <form class="layadmin-user-login-box layadmin-user-login-body layui-form" lay-filter="formLogin">
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
        <input type="text" name="mobile" autocomplete="off" id="LAY-user-login-username" lay-verify="required|phone"
          placeholder="用户名" class="layui-input">
      </div>
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
        <input type="password" name="password" autocomplete="off" id="LAY-user-login-password" lay-verify="required"
          placeholder="密码" class="layui-input">
      </div>
      <div class="layui-form-item" style="margin-bottom: 20px;">
        <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
        <!-- <a lay-href="/user/forget" class="layadmin-user-jump-change layadmin-link" style="margin-top: 7px;">忘记密码？</a> -->
      </div>
      <div class="layui-form-item">
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
      </div>
      <div class="layui-trans layui-form-item layadmin-user-login-other g-m--0 g-p--0">
      </div>
    </form>
  </div>

</div>

<script>
  layui.use(['admin', 'form', 'user'], function () {
    var $ = layui.$,
      setter = layui.setter,
      view = layui.view,
      baseUrl = setter.baseUrl,
      mockUrl = setter.mockUrl,
      admin = layui.admin,
      form = layui.form,
      router = layui.router(),
      search = router.search;


    var localTable = layui.data(setter.tableName),
      loginInfo = localTable.loginInfo;
    console.log(localTable)
    form.render();
    // console.log(layui.cache)
    // console.log(layui.setter)
    /* 记住密码 */
    if (loginInfo) {
      form.val('formLogin', loginInfo)
    }
    /* form.val('formLogin', {
      phone: '13888888888',
      password: 'admin_xcx'
    }) */
    //提交
    form.on('submit(LAY-user-login-submit)', function (obj) {
      // console.log(obj.field)
      //请求登入接口
      admin.req({
        url: `${baseUrl}admin/login/doLogin`, //实际使用请改成服务端真实接口
        dataType: 'json',
        type: 'POST',
        dataToken: false,
        data: obj.field,
        done: function (res) {
          var data = obj.field;
          // console.log(res)
          //请求成功后，写入 access_token
          layui.data(setter.tableName, {
            key: setter.request.tokenName,
            value: res.token
          });
          // 如果不勾选记住密码，就清空data中的账号和密码
          if (obj.field.remember !== 'on') {
            for (var key in data) {
              data[key] = ''
              console.log(data[key])
            }
          }
          // console.log(data)
          layui.data(setter.tableName, {
            key: 'loginInfo',
            value: data
          });

          //登入成功的提示与跳转
          layer.msg('登入成功', {
            offset: '15px',
            icon: 1,
            time: 1000
          }, function () {
            // console.log(decodeURIComponent(search.redirect))
            location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/';
          });
        }
      });
      return false;
    });


    //实际使用时记得删除该代码
    /* layer.msg('为了方便演示，用户名密码可随意输入', {
      offset: '15px',
      icon: 1
    }); */

  });
</script>